package dept;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.junit.Test;

import util.DBPoolUtils;

public class MenuDao {

	@Test
	public void insert() throws SQLException {
		Connection c = DBPoolUtils.getConnection();
		String sql = "insert into menu(name,price,spicy,deleteflag) values(?,?,?,false)";
		int row = DBPoolUtils.iud(c, sql, "拍黄瓜",5.9,false);
	}
	
	@Test
	public void update() throws SQLException {
		Connection c = DBPoolUtils.getConnection();
		String sql = "update menu set name=?,price=?,spicy=? where id=?";
		int row = DBPoolUtils.iud(c,sql,"拌三丁",9.9,true,4);
	}
	
	@Test
	public void delete() throws SQLException {
		Connection c = DBPoolUtils.getConnection();
		String sql = "update menu set deleteflag=true where id=? and deleteflag=false";
		int row = DBPoolUtils.iud(c,sql,4);
	}
	
	@Test
	public void selectById() throws SQLException {
		Connection c = DBPoolUtils.getConnection();
		String sql = "select * from menu where id=? and deleteflag=false";
		ResultSet set = DBPoolUtils.query(c, sql, 1);
		while(set.next()) {
			System.out.println(set.getInt(1) + "," + set.getString(2));
		}
	}
	
	@Test
	public void selectByName() throws SQLException {
		Connection c = DBPoolUtils.getConnection();
		String sql = "select * from menu where name like ? and deleteflag=false";
		ResultSet set = DBPoolUtils.query(c, sql, "%肉%");
		while(set.next()) {
			System.out.println(set.getInt(1) + "," + set.getString(2));
		}
	}
}
